package in.abip.chisame;

import in.abip.chisame.model.IListItem;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Class for all list view.
 * 
 * @author Julot.
 * @since Saturday, January 22nd, 2011.
 */
public class ListAdapter extends ArrayAdapter<Object> {
	protected List<Object> _objects;
    protected int _viewResourceId;
    protected LayoutInflater _inflater;
    
    /**
     * Constructor.
     * @param context Activity instance.
     * @param viewResourceId layout of list item.
     * @param objects {@link List<Object>} in most case this will be list of
     *        implementation of ActiveRecord abstract class. 
     */
	public ListAdapter(
		Context context,
		int viewResourceId,
		List<Object> objects
	) {
		super(context, viewResourceId, objects);
        _viewResourceId = viewResourceId;
		this._objects = objects;
		_inflater = (LayoutInflater) context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE
        );
	}
	
    /**
     * Change the image of list item according to room type.
     */
	public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) view = _inflater.inflate(_viewResourceId, null);
        IListItem item = (IListItem) _objects.get(position);
        TextView text = (TextView) view.findViewById(R.id.text);
        text.setText(item.getListText());
        text.setTag(item);
        ImageView image = (ImageView) view.findViewById(R.id.image);
        image.setImageResource(item.getListIconResource());
        image.setTag(item);
        return view;
    }
	
	/**
	 * @return items {@link List<Object>} 
	 */
	public List<Object> getItems() {
		return _objects;
	}
	
}
